package com.itcast.report

import com.itcast.utils.{ConfigHandler, MysqlHandler, RptKpiTools}
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.sql.SQLContext

object RptMediaAnalysisRedis {
  def main(args: Array[String]): Unit = {
    //创建sparkConf
    val sparkConf = new SparkConf()
      .setAppName("ReqMediaAnalysis")
      .setMaster("local[*]")
      .set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
    //创建sparkContext
    val sc = new SparkContext(sparkConf)
    //创建sqlContext实例
    val sQLContext = new SQLContext(sc)
    //读取数据
    val rawDataFrame = sQLContext.read.parquet(ConfigHandler.parquetPath)
    //过滤不为空的数据
      .filter("appid!=null or appid!='' or appname!=null or appname!=''")
    rawDataFrame.registerTempTable("log")
    sQLContext.udf.register("NotEmptyAppName",RptKpiTools.notEmptyAppName)
    val result = sQLContext.sql(
      """
        |select NotEmptyAppName(appid,appname) appName
        |sum(case when requestmode=1 and processnode>=1 then 1 else 0 end) rawReq,
        |sum(case when requestmode=1 and processnode>=2 then 1 else 0 end) effReq,
        |sum(case when requestmode=1 and processnode=3 then 1 else 0 end) adReq,
        |sum(case when iseffective=1 and isbilling=1 and isbid=1 and adorderid!=0 then 1 else 0 end) rtbReq,
        |sum(case when iseffective=1 and isbilling=1 and iswin=1 then 1 else 0 end) winReq,
        |sum(case when requestmode=2 and iseffective=1 then 1 else 0 end) adShow,
        |sum(case when requestmode=3 and iseffective=1 then 1 else 0 end) adClick,
        |sum(case when iseffective=1 and isbilling=1 and iswin = 1 then winprice/1000 else 0 end) adCost,
        |sum(case when iseffective=1 and isbilling=1 and iswin=1 then adpayment/1000 else 0 end) adpayment
        |from log group by NotEmptyAppName(appid,appname)
      """.stripMargin)

    MysqlHandler.save2db(result,ConfigHandler.mediatable)
    sc.stop()
  }
}
